package fr.sedoo.sos.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ObservationStation extends IsLocalized {
	
	private String urn;
	private String shortName;
	private String longName;
	private String description;
	private List<Sensor> sensors = new ArrayList<Sensor>();
	
	private String featureOfInterest;
	
	private ExperimentalSite site;
	
	public String getUrn() throws IllegalArgumentException
	{
		if (urn != null)
		{
			return urn;
		}
		else
		{
			try
			{
				return ("urn:"+getExperimentalSite().getObservatory().getProgram().getId()+":station:"+getExperimentalSite().getObservatory().getId()+":"+getExperimentalSite().getId()+"_"+getId()).toLowerCase();
			}
			catch (NullPointerException e)
			{
				throw new IllegalArgumentException("Impossible de constituer l'URN");
			}
		}
	}
	
	public void setUrn(String urn) {
		this.urn = urn;
	}
	public String getShortName() {
		return shortName;
	}
	public void setShortName(String shortName) {
		this.shortName = shortName;
	}
	public String getLongName() {
		return longName;
	}
	public void setLongName(String longName) {
		this.longName = longName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	public List<Sensor> getSensors() {
		return sensors;
	}
	
	public void addSensor(Sensor sensor)
	{
		sensors.add(sensor);
		sensor.setStation(this);
	}
	
	public String getOffering() 
	{
		return getUrn();
	}

	public ExperimentalSite getExperimentalSite() {
		return site;
	}
	public void setExperimentalSite(ExperimentalSite site) {
		this.site = site;
	}
	
	public Set<String> getObservedProperties()
	{
		Set<String> observedProperties = new  HashSet<String>();

		Iterator<Sensor> sensorIterator = getSensors().iterator();
		while (sensorIterator.hasNext()) 
		{
			Sensor sensor = sensorIterator.next();
			Iterator<Phenomenon> phenomenonIterator = sensor.getPhenomena().iterator();
			while (phenomenonIterator.hasNext()) 
			{
				observedProperties.add(phenomenonIterator.next().getUrn());
			}
		}
		return observedProperties;
	}

	@Override
	public IsLocalized getContainer() {
		return null;
	}

	public String getFeatureOfInterest() {
		return featureOfInterest;
	}

	public void setFeatureOfInterest(String featureOfInterest) {
		this.featureOfInterest = featureOfInterest;
	}
	

}
